//
//  YYSchoolViewController.m
//  drivingSchool
//
//  Created by 子yu 刘 on 2018/7/30.
//  Copyright © 2018年 子yu 刘. All rights reserved.
//

#import "YYSchoolViewController.h"
#import "YYCourseCell.h"
#import "YYTeacherCell.h"
#import "YYFillInformationVC.h"
#import "YYTeacherVC.h"

@interface YYSchoolViewController ()<UITableViewDelegate,UITableViewDataSource,SDCycleScrollViewDelegate>
@property(nonatomic ,strong)NSDictionary * dataDict;
@property(nonatomic ,strong)NSArray * priceArr;
@property(nonatomic ,strong)NSArray * teacherArr;
@property(nonatomic ,assign) NSInteger selIndex;
@property (strong, nonatomic) SDCycleScrollView *adCycleScrollView;


@end

@implementation YYSchoolViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getData];
    self.selIndex = -1;
    self.teacheTableView.layer.shadowOffset = CGSizeMake(1, 1);
    self.teacheTableView.layer.shadowOpacity = 0.8;
    self.teacheTableView.layer.shadowColor = [UIColor colorWithWhite:0.7 alpha:1].CGColor;
   // self.tableHeight.constant = 240;
  //  self.viewTop.constant = DEF_SCREEN_WIDTH()/5*3;
   // self.tableHeight.constant  = DEF_SCREEN_HEIGHT() - DEF_NAV_HEIGHT()-DEF_TABBAR_HEIGHT()-DEF_SCREEN_WIDTH()/5*3;
    self.tabWidth.constant = DEF_SCREEN_WIDTH();
    self.segment.sectionTitles = @[@"驾校课程", @"驾校教练"];
    self.segment.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor colorWithHexString:@"666666"],NSFontAttributeName:[UIFont systemFontOfSize:16.0f]};
    self.segment.selectedTitleTextAttributes = @{NSForegroundColorAttributeName : [UIColor colorWithHexString:@"333333"],NSFontAttributeName:[UIFont systemFontOfSize:16.0f]};
    self.segment.selectionIndicatorColor = DEF_COLOR_MAIN;
    self.segment.selectionIndicatorHeight = 2;
    self.segment.selectionStyle = HMSegmentedControlSelectionStyleTextWidthStripe;
    self.segment.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
    __weak typeof(self) weakSelf = self;
    [weakSelf.segment setIndexChangeBlock:^(NSInteger index) {
        CGFloat height = 0.0;
        if (index == 0) {
            height = self.priceArr.count*72;
            self.scorllHeight.constant = self.priceArr.count*72;
        }else{
            height = self.teacherArr.count*80+25;
             self.scorllHeight.constant = self.teacherArr.count*80+25;
        }
        [self.view layoutIfNeeded];
       [self.tableScrView scrollRectToVisible:CGRectMake(DEF_SCREEN_WIDTH()*index, 0, DEF_SCREEN_WIDTH(), height) animated:YES];
    }];
}

- (void)getData{
    [RequestOperationManager schoolDetail_sid:self.sid Success:^(id result) {
        if (DEF_REQ_SUCCESS) {
            self.dataDict = result[@"data"][@"list"];
            self.priceArr = result[@"data"][@"price"];
            self.teacherArr = result[@"data"][@"teacher"];
            [self reloadData];
        }
    } failture:^(NSError *error) {
        
    }];
}

- (void)reloadData{
  //  [self.image sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",DEF_INTERFACE_WEBVIEWSERVICE,_dataDict[@"image"]]]];
    NSMutableArray * imgArr = [NSMutableArray arrayWithCapacity:0];
    NSArray * arr = self.dataDict[@"image"];
    for (NSString * str in arr) {
        [imgArr addObject:[NSString stringWithFormat:@"%@%@",DEF_INTERFACE_WEBVIEWSERVICE,str]];
    }
    self.adCycleScrollView.imageURLStringsGroup = imgArr;
    self.lbName.text = _dataDict[@"school_name"];
    self.lbAdress.text = _dataDict[@"school_address"];
    [self.tyoeTableView reloadData];
    [self.teacheTableView reloadData];
    
}

- (IBAction)signUp:(id)sender {//报名
    if (!DEF_UID) {
        [[AppDelegate shareAppDelegate] enterLogin];
        return;
    }
    
    
    [AlertUtil showMessage:nil toView:self.view];
    [RequestOperationManager checkSchool_success:^(id result) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
        if (DEF_REQ_SUCCESS) {
            if (self.selIndex<0) {
                [AlertUtil alertMessage:@"请选择驾校课程"];
                [self.segment setSelectedSegmentIndex:0];
                  [self.tableScrView scrollRectToVisible:CGRectMake(0, 0, DEF_SCREEN_WIDTH(), self.tableScrView.frame.size.height) animated:YES];
                return;
            }
            YYFillInformationVC * fillVc = (YYFillInformationVC *)[CFUtility getVC:@"YYFillInformationVC" Storyboard:HOME];
            fillVc.schoolId = self.dataDict[@"id"];
            fillVc.price_id = self.priceArr[self.selIndex][@"id"];
        fillVc.money = self.priceArr[self.selIndex][@"money"];
            [self.navigationController pushViewController:fillVc animated:YES];
        }else{
            [AlertUtil alertMessage:result[@"msg"]];
        }
    } failture:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

- (IBAction)call:(id)sender {
    NSString *callPhone = [NSString stringWithFormat:@"telprompt://%@",self.dataDict[@"school_phone"]];
    if (@available(iOS 10.0, *)) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone] options:@{} completionHandler:nil];
    } else {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone]];
    }
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if ([tableView isEqual:self.tyoeTableView]) {
        return self.priceArr.count;
    }else if ([tableView isEqual:self.teacheTableView]){
        return self.teacherArr.count;
    }
    return 0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([tableView isEqual:self.tyoeTableView]) {
        YYCourseCell * cell = [tableView dequeueReusableCellWithIdentifier:@"YYCourseCell"];
        NSDictionary * itme = self.priceArr[indexPath.row];
        cell.lbType.text = itme[@"card_type"];
        cell.lbPrice.text = itme[@"money"];
        if (indexPath.row == _selIndex) {
            [cell.btnImg setImage:DEF_IMAGENAME(@"sign_inspection vehicle_checkbox") forState:UIControlStateNormal];
        }else{
             [cell.btnImg setImage:DEF_IMAGENAME(@"sign_inspection vehicle_checkbox_anselected") forState:UIControlStateNormal];
        }
        return cell;
    }else if([tableView isEqual:self.teacheTableView]){
        YYTeacherCell * cell = [tableView dequeueReusableCellWithIdentifier:@"YYTeacherCell"];
        NSDictionary * itme = self.teacherArr[indexPath.row];
        cell.lbname.text = itme[@"teacher_name"];
        [cell.icon sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",DEF_INTERFACE_WEBVIEWSERVICE,itme[@"teacher_image"]]]];
        cell.lbPhone.text = itme[@"teacher_phone"];
        cell.lbYear.text = [NSString stringWithFormat:@"%@年",itme[@"teacher_age"]];
        cell.grade = [itme[@"grade"] intValue];
        return cell;
    }
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([tableView isEqual:self.tyoeTableView]) {
        return 72;
    }else if([tableView isEqual:self.teacheTableView]){
        return 80;
    }
    return 0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([tableView isEqual:self.tyoeTableView]) {
        _selIndex = indexPath.row;
        [self.tyoeTableView reloadData];
    }else if([tableView isEqual:self.teacheTableView]){
        YYTeacherVC * teacher = (YYTeacherVC *)[CFUtility getVC:@"YYTeacherVC" Storyboard:HOME];
        teacher.teacherDic = self.teacherArr[indexPath.row];
        teacher.schooledDic = self.dataDict;
        [self.navigationController pushViewController:teacher animated:YES];
    }
}


-(NSDictionary *)dataDict{
    if (!_dataDict) {
        _dataDict = [NSDictionary dictionary];
    }
    return _dataDict;
}

-(NSArray *)priceArr{
    if (!_priceArr) {
        _priceArr = [NSArray array];
    }
    return _priceArr;
}

-(NSArray *)teacherArr{
    if (!_teacherArr) {
        _teacherArr = [NSArray array];
    }
    return _teacherArr;
}

-(SDCycleScrollView *)adCycleScrollView{
    if (!_adCycleScrollView) {
        _adCycleScrollView =  [SDCycleScrollView cycleScrollViewWithFrame:self.bannerView.bounds delegate:self placeholderImage:[UIImage new]];
        _adCycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
        _adCycleScrollView.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _adCycleScrollView.currentPageDotColor = [UIColor colorWithHexString:@"08c5ed"];
        _adCycleScrollView.pageDotColor = [UIColor colorWithHexString:@"bdbdbd"];
        _adCycleScrollView.pageControlDotSize = CGSizeMake(4, 4);  // pageControl小点的大小
        _adCycleScrollView.contentMode = UIViewContentModeScaleAspectFill;
        [self.bannerView addSubview:_adCycleScrollView];
        [_adCycleScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(0);
        }];
    }
    return _adCycleScrollView;
}
@end
